//
//  MissedNumViewController.m
//  NewProject
//
//  Created by apple on 2018/6/1.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "MissedNumViewController.h"
#import "HistogramView.h"

@interface MissedNumViewController ()


@property (weak, nonatomic) IBOutlet UIView *buttonView;


@property (weak, nonatomic) IBOutlet UIView *histogramBoxView_1;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *histogramWidth_1;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *space_1;

@property (weak, nonatomic) IBOutlet UIButton *firstButton;

@property (weak, nonatomic) IBOutlet UIButton *secondButton;

@property (weak, nonatomic) IBOutlet UIButton *thirdButton;


@property (weak, nonatomic) IBOutlet UIView *histogramBoxView_2;

@property (weak, nonatomic) IBOutlet UIView *histogramBoxView_3;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *histogramWidth_2;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *histogramWidth_3;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeightCons;

@end

@implementation MissedNumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"遗漏冷热";
    [self initWithLottery:[[LotteryManager sharedManager].currentLotteryID integerValue]];
}

-(void)initWithLottery:(NSInteger)lotteryID{
    NSInteger buttonTag = 0;
    switch (lotteryID) {
        case 3:
            buttonTag = 102;
            break;
        case 4:
            buttonTag = 101;
            break;
        case 5:
            buttonTag = 103;
            break;
        case 6:
            buttonTag = 104;
            break;
        case 7:
            buttonTag = 106;
            break;
        case 9:
            buttonTag = 105;
            break;
        default:
            break;
    }
    
    for (UIButton* button in self.buttonView.subviews) {
        [button setTitleColor:kRGBColor(102,102,102) forState:UIControlStateNormal];
    }
    UIButton* selectedButton = (UIButton*)[self.buttonView viewWithTag:buttonTag];
    [selectedButton setTitleColor:kRGBColor(255,102,0) forState:UIControlStateNormal];
    
    [self setNumberWithTag:buttonTag];
}

-(void)loadHistogramBoxOneWith:(NSString*)lotteryID{
    

    
    for (UIView* view in self.histogramBoxView_1.subviews) {
        [view removeFromSuperview];
    }
    NSArray* redArray = [[SelectionNumManager shared] redNumberArrayWith:lotteryID];
    CGFloat width = ([redArray count]<15)?20:15;;
    CGFloat height = self.histogramBoxView_1.frame.size.height;
    CGFloat spaceX = ([redArray count]<15)?20:15;;
    
    [self.firstButton setTitle:@"红球区" forState:UIControlStateNormal];
    
    
    if ([lotteryID isEqualToString:@"5"]||[lotteryID isEqualToString:@"6"]||[lotteryID isEqualToString:@"9"]) {
        [self.firstButton setTitle:@"百位区" forState:UIControlStateNormal];
        redArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    }
    if ([lotteryID isEqualToString:@"7"]) {
        [self.firstButton setTitle:@"11选5" forState:UIControlStateNormal];
    }
    
    self.histogramWidth_1.constant = [redArray count]*(width+spaceX)+20;
    
    
    
    NSMutableArray* saveArray = [NSMutableArray arrayWithCapacity:0];//保存今天的随机高度
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSDate* nowDate = [NSDate date];
    NSString* keyStringForToday = [formatter stringFromDate:nowDate];
    
    NSString* saveKey = [NSString stringWithFormat:@"%@-%@-one",keyStringForToday,lotteryID];
    NSArray* checkArray = [[NSUserDefaults standardUserDefaults] objectForKey:saveKey];
    
    BOOL newAdd = NO;
    if ([checkArray count]>0) {
        
    }else{
        newAdd = YES;
    }
    
    for (int i=0; i<[redArray count]; i++) {
        HistogramView* view = [[UINib nibWithNibName:@"Histogram" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
        view.frame = CGRectMake((width+spaceX)*i+10, 0, width, height);
        
        CGFloat newHeight = 0;
        if (newAdd) {
            NSInteger randomCount = arc4random()%100+1;
            newHeight = (randomCount/300.0f)*140;
            [saveArray addObject:@{redArray[i]:@(newHeight)}];
        }else{
            NSDictionary* oldDic = checkArray[i];
            newHeight = [[oldDic objectForKey:redArray[i]] floatValue];
        }
        view.insideViewCons.constant = newHeight;
        view.numberLabel.text = [NSString stringWithFormat:@"%@",[redArray objectAtIndex:i]];
        [self.histogramBoxView_1 addSubview:view];
    }
    
    
    [[NSUserDefaults standardUserDefaults] setObject:saveArray forKey:saveKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

-(void)loadHistogramBoxTwoWith:(NSString*)lotteryID{
    for (UIView* view in self.histogramBoxView_2.subviews) {
        [view removeFromSuperview];
    }
    [self.secondButton setTitle:@"蓝球区" forState:UIControlStateNormal];
    [self.secondButton setTitleColor:kRGBColor(66,151,255) forState:UIControlStateNormal];
    self.secondButton.backgroundColor = kRGBColor(235,244,255);
    NSArray* blueArray = [[SelectionNumManager shared] blueNumberArrayWith:lotteryID];
    
    UIColor* showColor = kRGBColor(66,151,255);
    if ([lotteryID isEqualToString:@"5"]||[lotteryID isEqualToString:@"6"]||[lotteryID isEqualToString:@"9"]) {
        [self.secondButton setTitle:@"十位区" forState:UIControlStateNormal];
        [self.secondButton setTitleColor:kRGBColor(255,114,12) forState:UIControlStateNormal];
        self.secondButton.backgroundColor = kRGBColor(255,239,228);
        blueArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
        showColor = kRGBColor(255,102,0);
    }
    
    CGFloat width = ([blueArray count]<15)?20:15;;
    CGFloat height = self.histogramBoxView_2.frame.size.height;
    CGFloat spaceX = ([blueArray count]<15)?20:15;
    
    self.histogramWidth_2.constant = [blueArray count]*(width+spaceX)+20;
    
    
    NSMutableArray* saveArray = [NSMutableArray arrayWithCapacity:0];//保存今天的随机高度
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSDate* nowDate = [NSDate date];
    NSString* keyStringForToday = [formatter stringFromDate:nowDate];
    
    NSString* saveKey = [NSString stringWithFormat:@"%@-%@-two",keyStringForToday,lotteryID];
    NSArray* checkArray = [[NSUserDefaults standardUserDefaults] objectForKey:saveKey];
    
    BOOL newAdd = NO;
    if ([checkArray count]>0) {
        
    }else{
        newAdd = YES;
    }
    
    
    for (int i=0; i<[blueArray count]; i++) {
        HistogramView* view = [[UINib nibWithNibName:@"Histogram" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
        view.frame = CGRectMake((width+spaceX)*i+10, 0, width, height);
        CGFloat newHeight = 0;
        if (newAdd) {
            NSInteger randomCount = arc4random()%100+1;
            newHeight = (randomCount/300.0f)*140;
            [saveArray addObject:@{blueArray[i]:@(newHeight)}];
        }else{
            NSDictionary* oldDic = checkArray[i];
            newHeight = [[oldDic objectForKey:blueArray[i]] floatValue];
        }
        view.insideViewCons.constant = newHeight;
        view.insideView.backgroundColor = showColor;
        view.numberLabel.text = [NSString stringWithFormat:@"%@",[blueArray objectAtIndex:i]];
        view.numberLabel.textColor = showColor;
        [self.histogramBoxView_2 addSubview:view];
    }
    [[NSUserDefaults standardUserDefaults] setObject:saveArray forKey:saveKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(void)loadHistogramBoxThreeWith:(NSString*)lotteryID{
    for (UIView* view in self.histogramBoxView_3.subviews) {
        [view removeFromSuperview];
    }
    
    NSArray* blueArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    
    CGFloat width = ([blueArray count]<15)?20:15;
    CGFloat height = self.histogramBoxView_3.frame.size.height;
    CGFloat spaceX = ([blueArray count]<15)?20:15;
    
    self.histogramWidth_3.constant = [blueArray count]*(width+spaceX)+20;
    
    
    NSMutableArray* saveArray = [NSMutableArray arrayWithCapacity:0];//保存今天的随机高度
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSDate* nowDate = [NSDate date];
    NSString* keyStringForToday = [formatter stringFromDate:nowDate];
    
    NSString* saveKey = [NSString stringWithFormat:@"%@-%@-three",keyStringForToday,lotteryID];
    NSArray* checkArray = [[NSUserDefaults standardUserDefaults] objectForKey:saveKey];
    
    BOOL newAdd = NO;
    if ([checkArray count]>0) {
        
    }else{
        newAdd = YES;
    }
    
    for (int i=0; i<[blueArray count]; i++) {
        HistogramView* view = [[UINib nibWithNibName:@"Histogram" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
        view.frame = CGRectMake((width+spaceX)*i+10, 0, width, height);
        CGFloat newHeight = 0;
        if (newAdd) {
            NSInteger randomCount = arc4random()%100+1;
            newHeight = (randomCount/300.0f)*140;
            [saveArray addObject:@{blueArray[i]:@(newHeight)}];
        }else{
            NSDictionary* oldDic = checkArray[i];
            newHeight = [[oldDic objectForKey:blueArray[i]] floatValue];
        }
        view.insideViewCons.constant = newHeight;
        view.insideView.backgroundColor = kRGBColor(255,102,0);;
        view.numberLabel.text = [NSString stringWithFormat:@"%@",[blueArray objectAtIndex:i]];
        view.numberLabel.textColor = kRGBColor(255,102,0);
        [self.histogramBoxView_3 addSubview:view];
    }
    [[NSUserDefaults standardUserDefaults] setObject:saveArray forKey:saveKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setNumberWithTag:(NSInteger)buttonTag{
    NSString* lotteryID = @"";
    NSInteger boxCount = 2;
    switch (buttonTag) {
        case 101:
            lotteryID = @"4";
            boxCount = 2;
            break;
        case 102:
            lotteryID = @"3";
            boxCount = 2;
            break;
        case 103:
            lotteryID = @"5";
            boxCount = 3;
            break;
        case 104:
            lotteryID = @"6";
            boxCount = 3;
            break;
        case 105:
            lotteryID = @"9";
            boxCount = 3;
            break;
        case 106:
            lotteryID = @"7";
            boxCount = 1;
            break;
        default:
            break;
    }
    
    self.viewHeightCons.constant = 70+246*boxCount+20;
    
    switch (boxCount) {
        case 1:
            self.secondButton.hidden = YES;
            self.thirdButton.hidden = YES;
            
            self.histogramBoxView_2.hidden = YES;
            self.histogramBoxView_3.hidden = YES;
            
            [self loadHistogramBoxOneWith:lotteryID];
            break;
        case 2:
            self.secondButton.hidden = NO;
            self.thirdButton.hidden = YES;
            
            self.histogramBoxView_2.hidden = NO;
            self.histogramBoxView_3.hidden = YES;
            
            [self loadHistogramBoxOneWith:lotteryID];
            [self loadHistogramBoxTwoWith:lotteryID];
            break;
        case 3:
            self.secondButton.hidden = NO;
            self.thirdButton.hidden = NO;
            
            self.histogramBoxView_2.hidden = NO;
            self.histogramBoxView_3.hidden = NO;
            [self loadHistogramBoxOneWith:lotteryID];
            [self loadHistogramBoxTwoWith:lotteryID];
            [self loadHistogramBoxThreeWith:lotteryID];
            break;
        default:
            break;
    }
    
    
}

- (IBAction)buttonAction:(UIButton *)sender {
    for (UIButton* button in self.buttonView.subviews) {
        [button setTitleColor:kRGBColor(102,102,102) forState:UIControlStateNormal];
    }
    [sender setTitleColor:kRGBColor(255,102,0) forState:UIControlStateNormal];
    
    [self setNumberWithTag:sender.tag];
    
}








/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
